package com.itheima.crm.controller;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itheima.common.utils.Page;
import com.itheima.crm.pojo.BaseDict;
import com.itheima.crm.pojo.RiZhi;
import com.itheima.crm.pojo.YuanGong;
import com.itheima.crm.service.BaseDictService;

import com.itheima.crm.service.OrganizationService;
import com.itheima.crm.service.RiZhiService;
import com.itheima.crm.service.YuanGongService;

/**
 * 客户管理
 * 
 * @author lx
 *
 */
@Controller
public class YuanGongController {

	@Autowired
	private BaseDictService baseDictService;
	@Autowired
	private YuanGongService yuangongService;
	@Autowired
	private OrganizationService organizationService;
	@Autowired
	private RiZhiService rizhiService;
	
	// 注解在成员变量上

	public BaseDictService getBaseDictService() {
		return baseDictService;
	}

	public void setBaseDictService(BaseDictService baseDictService) {
		this.baseDictService = baseDictService;
	}

	public YuanGongService getYuangongService() {
		return yuangongService;
	}

	public void setYuangongService(YuanGongService yuangongService) {
		this.yuangongService = yuangongService;
	}

	public OrganizationService getOrganizationService() {
		return organizationService;
	}

	public void setOrganizationService(OrganizationService organizationService) {
		this.organizationService = organizationService;
	}

	public RiZhiService getRizhiService() {
		return rizhiService;
	}

	public void setRizhiService(RiZhiService rizhiService) {
		this.rizhiService = rizhiService;
	}

	// 入口
	@RequestMapping(value = "/yuangong")
	public String list(YuanGong vo, Model model,HttpServletRequest request) {
		HttpSession session=request.getSession(false);
		String zhiwei = (String) session.getAttribute("zhiwei");
		
		if (!zhiwei.equals("1")) {
		Integer id=yuangongService.MaxId()+1;
		
		System.out.println(id);
		request.getSession().setAttribute("yuangong_ygidinsert",id);
		
		List<BaseDict> SexType = baseDictService.selectBaseDictListByCode("001");
		List<BaseDict> OrganizationType = baseDictService.selectBaseDictListByCode("002");
		List<BaseDict> zhiweiType = baseDictService.selectBaseDictListByCode("003");
	
		
		model.addAttribute("SexType", SexType);
		model.addAttribute("OrganizationType", OrganizationType);
		model.addAttribute("zhiweiType", zhiweiType);

		// 通过四个条件 查询分页对象
		
		Page<YuanGong> page = yuangongService.selectPageByQueryVo(vo);
		
		model.addAttribute("page",page);
		
		// 修改员工人数后自动刷新部门表的人数
		organizationService.updateOrganizationCount();
		
		return "YuanGong";}
		else {
			return "error";
		}
	}
	
	@RequestMapping(value = "/GuanLi")
	public ModelAndView GuanLilogin(YuanGong yuangong, Model model, HttpServletRequest request) {

		YuanGong result = yuangongService.selectLogin(yuangong);

		if (yuangong.getLoginname().equals(result.getLoginname())
				&& yuangong.getLoginpw().equals(result.getLoginpw())&&result.getZhiwei().equals("3")) {
			System.out.println("相同");
			ModelAndView a = new ModelAndView();
			request.getSession().setAttribute("loginname", yuangong.getLoginname());
			request.getSession().setAttribute("loginpw", yuangong.getLoginpw());
			request.getSession().setAttribute("ygname", result.getYgname());
			request.getSession().setAttribute("YGid", result.getYgid());
			request.getSession().setAttribute("zhiwei", result.getZhiwei());
			request.getSession().setAttribute("organziation", result.getOrganization());
			a.setViewName("Main");
			
			return a;

		}

		ModelAndView a = new ModelAndView("login");

		return a;

	}


	// 去修改页面
	@RequestMapping(value = "/yuangong/edit.action")
	public @ResponseBody YuanGong edit(Integer id) {

		return yuangongService.selectYuanGongById(id);
	}

	// 修改保存
	@RequestMapping(value = "/yuangong/update.action")
	public @ResponseBody JSONObject update(YuanGong yuangong) {
		JSONObject obj=new JSONObject();
		   Integer id=yuangong.getId();
		 YuanGong oldyuangong=yuangongService.selectYuanGongById(id);
		if(oldyuangong.getZhiwei().equals("2")) {
			
			if(!oldyuangong.getOrganization().equals(yuangong.getOrganization())) {
				String message="此人为部门经理，不能调部门，请再管理员页面先改为员工后，指定完新的部门经理，再进行部门调整";
				obj.put("message", message);
				return obj;
			}
		}
		RiZhi a=new RiZhi();
		a.setYgid(yuangong.getYgid());
		a.setYgname(yuangong.getYgname());
		a.setInsertdate(new Date());
		System.out.println(a.getInsertdate());
		a.setDetial("修改员工信息");
		rizhiService.insertApplication(a);
		// 修改
		yuangongService.updateYuanGongById(yuangong);
		obj.put("message","OK");
		return obj;
	}

	// 删除
	@RequestMapping(value = "/yuangong/delete.action")
	public @ResponseBody String delete(Integer id) {
		YuanGong yuangong = yuangongService.selectYuanGongById(id);
		yuangongService.deleteYuanGongById(id);
		// 如果在员工表中查不到这个对象
		String org = yuangong.getOrganization();
		Integer selectYuanGongHaveAfterDelete = yuangongService.selectYuanGongHaveAfterDelete(org);
		if (selectYuanGongHaveAfterDelete == 0) {
			organizationService.updateOrganizationCountto0(org);
		}
		RiZhi a=new RiZhi();
		a.setYgid(yuangong.getYgid());
		a.setYgname(yuangong.getYgname());
		a.setInsertdate(new Date());
		System.out.println(a.getInsertdate());
		a.setDetial("删除员工信息");
		rizhiService.insertApplication(a);
		return "OK";
	}

	// 添加
	@RequestMapping(value = "/yuangong/insert.action")
	public @ResponseBody String insert(YuanGong yuangong) {
		yuangongService.insertYuanGong(yuangong);
		RiZhi a=new RiZhi();
		a.setYgid(yuangong.getYgid());
		a.setYgname(yuangong.getYgname());
		a.setInsertdate(new Date());
		System.out.println(a.getInsertdate());
		a.setDetial("添加员工信息");
		rizhiService.insertApplication(a);

		return "OK";
	}

	@RequestMapping(value = "/yuangong/selectManager.action", method = RequestMethod.POST)
	public @ResponseBody String selectManager(HttpServletRequest request) throws UnsupportedEncodingException {

		String ora = request.getParameter("organization");

		String manager = yuangongService.selectManagerByYuanGong(ora);

		String json = JSON.toJSONString(manager);

		return json;
	}
	
	


}
